<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2008-03-03"
  version="1.0">
<xsl:output method="xhtml" encoding="utf-8"/>

<xsl:variable name="Arguments" select="/aws:ItemSearchResponse/aws:OperationRequest/aws:Arguments" />
<xsl:variable
   name="SearchIndex"
   select="$Arguments/aws:Argument[@Name='SearchIndex']/@Value" />
<xsl:variable
   name="AssociateTag"
   select="$Arguments/aws:Argument[@Name='AssociateTag']/@Value" />
<xsl:variable
   name="ItemPage"
   select="$Arguments/aws:Argument[@Name='ItemPage']/@Value" />
<xsl:variable
   name="Service"
   select="$Arguments/aws:Argument[@Name='Service']/@Value" />
<xsl:variable
   name="Keywords"
   select="$Arguments/aws:Argument[@Name='Keywords']/@Value" />
<xsl:variable
   name="ResponseGroup"
   select="$Arguments/aws:Argument[@Name='ResponseGroup']/@Value" />
<xsl:variable
   name="Operation"
   select="$Arguments/aws:Argument[@Name='Operation']/@Value" />
<xsl:variable
   name="Style"
   select="$Arguments/aws:Argument[@Name='Style']/@Value" />
<xsl:variable
   name="AWSAccessKeyId"
   select="$Arguments/aws:Argument[@Name='AWSAccessKeyId']/@Value" />
<xsl:variable
   name="Version"
   select="$Arguments/aws:Argument[@Name='Version']/@Value" />

<xsl:template match="/">
  <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <link rel="stylesheet" href="http://ogawa.googlecode.com/svn/blog/aws/style.css" type="text/css" />
      <title>AWSECommerceService: <xsl:value-of select="$Keywords" /></title>
    </head>
    <body>
      <h1>AWSECommerceService: <xsl:value-of select="$Keywords" /></h1>
      <xsl:apply-templates select="aws:ItemSearchResponse" />
      <xsl:call-template name="queryform" />
    </body>
  </html>
</xsl:template>

<xsl:template match="aws:ItemSearchResponse">
  <h2>Search Results</h2>
  <xsl:apply-templates select="aws:Items/aws:Item" />

  <div class="pagination">
    <ul>
      <xsl:call-template name="pagination">
	<xsl:with-param name="page" select="1" />
	<xsl:with-param name="totalPages" select="aws:Items/aws:TotalPages" />
      </xsl:call-template>
    </ul>
  </div>
</xsl:template>

<xsl:template match="aws:Items/aws:Item">
  <xsl:variable name="associate">
    <div class="associate">
      <a href="{aws:DetailPageURL}" target="_blank" rel="nofollow">
	<img src="{aws:ImageSets/aws:ImageSet/aws:MediumImage/aws:URL}" width="{aws:ImageSets/aws:ImageSet/aws:MediumImage/aws:Width}" height="{aws:ImageSets/aws:ImageSet/aws:MediumImage/aws:Height}" alt="{aws:ItemAttributes/aws:Title}" style="border: none;" /></a>
      <br />
      <a href="{aws:DetailPageURL}" target="_blank" rel="nofollow"><xsl:value-of select="aws:ItemAttributes/aws:Title" /></a>
      <img src="http://www.assoc-amazon.jp/e/ir?t={$AssociateTag}&amp;amp;l=as2&amp;amp;o=9&amp;amp;a={aws:ASIN}" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />
    </div>
  </xsl:variable>
  <div class="result">
    <h3>ASIN: <xsl:value-of select="aws:ASIN" /></h3>
    <div class="result-display">
      <xsl:copy-of select="$associate" />
    </div>
    <div class="result-text">
      <textarea cols="60" rows="15">
	<xsl:copy-of select="$associate" />
      </textarea>
    </div>
  </div>
</xsl:template>

<xsl:variable name="baseUrl">http://ecs.amazonaws.jp/onca/xml?Service=AWSECommerceService&amp;AWSAccessKeyId=<xsl:value-of select="$AWSAccessKeyId" />&amp;Operation=ItemSearch&amp;Version=<xsl:value-of select="$Version" />&amp;ContentType=text/html&amp;Style=<xsl:value-of select="$Style" />&amp;Keywords=<xsl:value-of select="$Keywords" />&amp;SearchIndex=<xsl:value-of select="$SearchIndex" />&amp;ResponseGroup=<xsl:value-of select="$ResponseGroup" />&amp;AssociateTag=<xsl:value-of select="$AssociateTag" /></xsl:variable>

<xsl:template name="pagination">
  <xsl:param name="page" />
  <xsl:param name="totalPages" />
  <xsl:if test="$page &lt;= $totalPages">
    <xsl:if test="$page = $ItemPage">
      <li><xsl:value-of select="$page" /></li>
    </xsl:if>
    <xsl:if test="$page != $ItemPage">
      <li><a href="{concat($baseUrl, '&amp;ItemPage=', $page)}"><xsl:value-of select="$page" /></a></li>
    </xsl:if>
    <xsl:variable name="newpage" select="$page + 1"/>
    <xsl:call-template name="pagination">
      <xsl:with-param name="page" select="$newpage" />
      <xsl:with-param name="totalPages" select="$totalPages" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template name="queryform">
  <div class="queryform">
    <form action="http://ecs.amazonaws.jp/onca/xml" method="get">
      <input type="hidden" name="Service" value="{$Service}" />
      <input type="hidden" name="AWSAccessKeyId" value="{$AWSAccessKeyId}" />
      <input type="hidden" name="Version" value="{$Version}" />
      <input type="hidden" name="Operation" value="{$Operation}" />
      <input type="hidden" name="ContentType" value="text/html" />
      <input type="hidden" name="ResponseGroup" value="{$ResponseGroup}" />
      <input type="hidden" name="ItemPage" value="1" />
      <input type="hidden" name="Style" value="http://ogawa.googlecode.com/svn/blog/aws/ecs.xsl" />

      <h2>Keywords</h2>
      <div><input type="text" name="Keywords" value="{$Keywords}" /></div>

      <h2>Associate ID</h2>
      <input type="text" name="AssociateTag" value="{$AssociateTag}" />

      <h2>Category</h2>
      <ul>
	<li><input type="radio" name="SearchIndex" value="Blended" checked="checked" />Blended</li>
	<li><input type="radio" name="SearchIndex" value="Books" />Books</li>
	<li><input type="radio" name="SearchIndex" value="ForeignBooks" />Foreign Books</li>
	<li><input type="radio" name="SearchIndex" value="Electronics" />Electronics</li>
	<li><input type="radio" name="SearchIndex" value="Kitchen" />Kitchen</li>
	<li><input type="radio" name="SearchIndex" value="Music" />Music</li>
	<li><input type="radio" name="SearchIndex" value="MusicTracks" />Music Tracks</li>
	<li><input type="radio" name="SearchIndex" value="Classical" />Classical</li>
	<li><input type="radio" name="SearchIndex" value="DVD" />DVD</li>
	<li><input type="radio" name="SearchIndex" value="Video" />DVD &amp; VHS</li>
	<li><input type="radio" name="SearchIndex" value="VHS" />VHS</li>
	<li><input type="radio" name="SearchIndex" value="Software" />Software</li>
	<li><input type="radio" name="SearchIndex" value="VideoGames" />Video Games</li>
	<li><input type="radio" name="SearchIndex" value="Toys" />Toys</li>
	<li><input type="radio" name="SearchIndex" value="SportingGoods" />Sporting Goods</li>
	<li><input type="radio" name="SearchIndex" value="HealthPersonalCare" />Health Personal Care</li>
      </ul>

      <p><input type="submit" name="button" value="Search" /></p>
    </form>
  </div>
</xsl:template>

</xsl:stylesheet>
